package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        //先查询出顶级菜单中的所有数据,父级为null
        // List<Region> list = regionService.selectByParentId(null);
        //PageInfo pageInfo = regionService.query(qo);
        //model.addAttribute("entity",list);
        //model.addAttribute("pageInfo",pageInfo);
        return "region/list";
    }

    @RequestMapping("/listByParentId")
    @ResponseBody
    public Object contory(Long parentId) {
        //先查询出顶级菜单中的所有数据,父级为null
        List<Region> list = regionService.selectByParentId(parentId);
        //new一个mapList里面装的都是MAP对象,用来存储
        List<Map<String, Object>> mapList = new ArrayList<>();
        //从list转为mapList
        //更改属性的名字,适应规则
        //Map<String,Object> map=new HashMap<>();
        //把对象都转化为map直接扔过去
        for (Region r : list) {
            mapList.add(r.toMap());
        }
        return mapList;
    }



    @RequestMapping("/save")
    @ResponseBody
    public Object saveOrUpdate(Region region) {
        JSONResult json = new JSONResult();

        regionService.save(region);
        return json;
    }
}
